package vn.vfriends.vbox.facade;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import vn.vfriends.vbox.model.Role;
import vn.vfriends.vbox.model.RoleCode;

/**
 *
 * @author tuancn
 */
@Stateless
public class RoleFacade extends AbstractFacade<Role> {
    
    @PersistenceContext(unitName = "vBoxPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    public RoleFacade() {
        super(Role.class);
    }

    public Role createRole(Role role) {
        super.create(role);
        return role;
    }

    public Role editRole(Role role) {
        super.edit(role);
        return role;
    }

    public void remove(Integer id) {
        super.remove(super.find(id));
    }

    public Role find(Integer id) {
        return super.find(id);
    }
    
    public Role findbyCode(String code) {
        Query query = em.createNamedQuery("Role.findByCode", Role.class);
        query.setParameter("code", RoleCode.valueOf(code));
        List<Role> roleList = query.getResultList();
        if(roleList != null && !roleList.isEmpty())
            return roleList.get(0);
        else
            return null;
    }
    
    public Role findbyName(String name) {
        Query query = em.createNamedQuery("Role.findByName", Role.class);
        query.setParameter("name", name);
        List<Role> roleList = query.getResultList();
        if(roleList != null && !roleList.isEmpty())
            return roleList.get(0);
        else
            return null;
    }

    @Override
    public List<Role> findAll() {
        return super.findAll();
    }

    public List<Role> findRange(Integer from, Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @Override
    public int count() {
        return super.count();
    }
}
